home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / demobook / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.7 KB  |  233 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include "exinterfmotif.h"
  19. #include <gl.h>
  20. #include <gl/image.h>
  21. #include <string.h>
  22. #include "exglobals.h"
  23. #include "exbookglo.h"
  24. #define NULLCHARPTR (char *)0
  25.  
  26.    float tex_props[] = {TX_MINFILTER,TX_MIPMAP_BILINEAR,
  27.                          TX_MAGFILTER,TX_BILINEAR,TX_NULL};
  28.    float env_props[] = {TV_NULL};
  29.  
  30. static long next_tex_id = 1;
  31. static void (*i_errfunc)();
  32.  
  33.  
  34. void init_texture(struct icntmpltstruct *iconptr)
  35. {
  36.    long id = 1;
  37.  
  38.    texdef2d(iconptr->tex_id, 4, iconptr->tex_width,
  39.                 iconptr->tex_height, iconptr->tex_image, 5, tex_props);
  40.    tevdef(id,1,env_props);
  41. }
  42.  
  43. void readicon_i(struct icntmpltstruct *iconptr)
  44. {
  45.     unsigned long *lptr;
  46.     short *rbuf, *gbuf, *bbuf, *abuf;
  47.     char strgin[50];
  48.     char *outstrg1;
  49.     char *outstrg2;
  50.     IMAGE *image;
  51.     int y;
  52.     char *fname;
  53.  
  54.      /* Make sure bad image files or improper image names don't crash 
  55.          and burn the program.... */
  56.      strcpy(strgin,iconptr->imgfile->name);
  57.      /* printf("iopen...strgin = %s\n",strgin); */
  58.  
  59.     /* if ((outstrg1 = strstr(strgin,".rgb")) == NULLCHARPTR) {
  60.         printf("not a valid image file....%s\n",outstrg1);
  61.           iconptr->imgfile = NULL;
  62.          return;
  63.     }  */
  64.     /* if ((outstrg2 = strstr(strgin,".icon")) == NULLCHARPTR) {
  65.         printf("not a valid image file....%s\n",outstrg2);
  66.          iconptr->imgfile = NULL;
  67.          return;
  68.     } */
  69.     if (demoenv != NULL && strncmp("$DEMOS", iconptr->imgfile->name, 6) == 0)
  70.        {
  71.        fname = (char *)malloc(demoenvlen + strlen(iconptr->imgfile->name) );
  72.        strcpy(fname, demoenv);
  73.        strcpy(&(fname[demoenvlen]), &(iconptr->imgfile->name[6]));
  74.        }
  75.     else if (xenv != NULL && strncmp("$XDEMOS",iconptr->imgfile->name, 7) == 0)
  76.        {
  77.        fname = (char *)malloc(xenvlen + strlen(iconptr->imgfile->name) );
  78.        strcpy(fname, xenv);
  79.        strcpy(&(fname[xenvlen]), &(iconptr->imgfile->name[7]));
  80.        }
  81.     else if (sbenv != NULL && strncmp("$SBIN", iconptr->imgfile->name, 5) == 0)
  82.        {
  83.        fname = (char *)malloc(sbenvlen + strlen(iconptr->imgfile->name) );
  84.        strcpy(fname, sbenv);
  85.        strcpy(&(fname[sbenvlen]), &(iconptr->imgfile->name[5]));
  86.        }
  87.     else
  88.        {
  89.        fname = (char *)malloc(1 + strlen(iconptr->imgfile->name) );
  90.        strcpy(fname, iconptr->imgfile->name);
  91.        }
  92. /*
  93.     image = iopen(iconptr->imgfile->name,"r");
  94. */
  95.     image = iopen(fname,"r");
  96.     free(fname);
  97.     if(!image)
  98.        {
  99.         sprintf(msgstring, "\nDemo %s\n Invalid image icon file :\n%s",
  100.                  iconptr->nameptr->string, iconptr->imgfile->name);
  101.         popup_Message();
  102.         handleMessageEvents();
  103.             iconptr->imgfile = NULL;
  104.             return;
  105.        }
  106.     else
  107.        {
  108.        iconptr->tex_image = (unsigned long *)malloc(image->xsize*image->ysize*sizeof(unsigned long));
  109.        rbuf = (short *)malloc(image->xsize*sizeof(short));
  110.        gbuf = (short *)malloc(image->xsize*sizeof(short));
  111.        bbuf = (short *)malloc(image->xsize*sizeof(short));
  112.        abuf = (short *)malloc(image->xsize*sizeof(short));
  113.        if(!iconptr->tex_image || !rbuf || !gbuf || !bbuf) {
  114.        fprintf(stderr,"reading icon image file : can't malloc enough memory\n");
  115.              iclose(image);
  116.                 iconptr->imgfile = NULL;
  117.                 return;
  118.           }
  119.        else
  120.           {
  121.           iconptr->tex_width = image->xsize;
  122.           iconptr->tex_height = image->ysize;
  123.           iconptr->tex_id = next_tex_id;
  124.           next_tex_id++;
  125.           lptr = iconptr->tex_image;
  126.           for(y=0; y<image->ysize; y++) {
  127.           if(image->zsize>=4)
  128.                  {
  129.               getrow(image,rbuf,y,0);
  130.               getrow(image,gbuf,y,1);
  131.               getrow(image,bbuf,y,2);
  132.               getrow(image,abuf,y,3);
  133.               rgbatocpack(rbuf,gbuf,bbuf,abuf,lptr,image->xsize);
  134.               lptr += image->xsize;
  135.              }
  136.               else if(image->zsize==3)
  137.                  {
  138.               getrow(image,rbuf,y,0);
  139.               getrow(image,gbuf,y,1);
  140.               getrow(image,bbuf,y,2);
  141.               rgbtocpack(rbuf,gbuf,bbuf,lptr,image->xsize);
  142.               lptr += image->xsize;
  143.              }
  144.               else 
  145.                   {
  146.               getrow(image,rbuf,y,0);
  147.               bwtocpack(rbuf,lptr,image->xsize);
  148.               lptr += image->xsize;
  149.                   }
  150.           }
  151.              }
  152.        iclose(image);
  153.        free(rbuf);
  154.        free(gbuf);
  155.        free(bbuf);
  156.        free(abuf);
  157.        init_texture(iconptr);
  158.       }
  159. }
  160.  
  161.  
  162. bwtocpack(b,l,n)
  163. register unsigned short *b;
  164. register unsigned long *l;
  165. register int n;
  166. {
  167.     while(n>=8) {
  168.     l[0] = 0x00010101*b[0];
  169.     l[1] = 0x00010101*b[1];
  170.     l[2] = 0x00010101*b[2];
  171.     l[3] = 0x00010101*b[3];
  172.     l[4] = 0x00010101*b[4];
  173.     l[5] = 0x00010101*b[5];
  174.     l[6] = 0x00010101*b[6];
  175.     l[7] = 0x00010101*b[7];
  176.     l += 8;
  177.     b += 8;
  178.     n -= 8;
  179.     }
  180.     while(n--) 
  181.     *l++ = 0x00010101*(*b++);
  182. }
  183.  
  184. rgbtocpack(r,g,b,l,n)
  185. register unsigned short *r, *g, *b;
  186. register unsigned long *l;
  187. register int n;
  188. {
  189.     while(n>=8) {
  190.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16);
  191.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16);
  192.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16);
  193.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16);
  194.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16);
  195.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16);
  196.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16);
  197.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16);
  198.     l += 8;
  199.     r += 8;
  200.     g += 8;
  201.     b += 8;
  202.     n -= 8;
  203.     }
  204.     while(n--) 
  205.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16);
  206. }
  207.  
  208. rgbatocpack(r,g,b,a,l,n)
  209. register unsigned short *r, *g, *b, *a;
  210. register unsigned long *l;
  211. register int n;
  212. {
  213.     while(n>=8) {
  214.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16) | (a[0]<<24);
  215.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16) | (a[1]<<24);
  216.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16) | (a[2]<<24);
  217.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16) | (a[3]<<24);
  218.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16) | (a[4]<<24);
  219.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16) | (a[5]<<24);
  220.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16) | (a[6]<<24);
  221.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16) | (a[7]<<24);
  222.     l += 8;
  223.     r += 8;
  224.     g += 8;
  225.     b += 8;
  226.     a += 8;
  227.     n -= 8;
  228.     }
  229.     while(n--) 
  230.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16) | ((*a++)<<24);
  231. }
  232.  
  233.